package com.mrli.cat.weather.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 天气信息的数据库
 */
public class WeatherDbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private  static final String DATABASE_NAME = "weather.db";

    public WeatherDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //CREATE TABLE location (
        // id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        // name text NOT NULL,
        // lat real NOT NULL,
        // lon real NOT NULL);
        final String SQL_CREATE_LOCATION_TABLE = "create table " + WeatherContract.LocationEntry.TABLE_NAME
                + " (" + WeatherContract.LocationEntry._ID + " integer primary key autoincrement, "
                + WeatherContract.LocationEntry.COLUMN_NAME + " text not null unique, "
                + WeatherContract.LocationEntry.COLUMN_LAT + " real not null, "
                + WeatherContract.LocationEntry.COLUMN_LON + " real not null"
                + " );";

        //CREATE TABLE weather (
        // id INTEGER PRIMARY KEY AUTOINCREMENT,
        // temp_max integer NOT NULL,
        // temp_min integer NOT NULL,
        // weather integer NOT NULL,
        // date long NOT NULL,
        // city_id integer NOT NULL,
        // FOREIGN KEY (city_id) REFERENCES location (id));
        final String SQL_CREATE_WEATHER_TABLE = "create table " + WeatherContract.WeatherEntry.TABLE_NAME
                + "( " + WeatherContract.WeatherEntry._ID + " integer primary key autoincrement, "
                + WeatherContract.WeatherEntry.COLUMN_TEMP_MAX + " integer not null, "
                + WeatherContract.WeatherEntry.COLUMN_TEMP_MIN + " integer not null, "
                + WeatherContract.WeatherEntry.COLUMN_TIME + " long not null, "
                + WeatherContract.WeatherEntry.COLUMN_WEATHER + " integer not null, "
                + WeatherContract.WeatherEntry.COLUMN_HUMI + " integer not null, "
                + WeatherContract.WeatherEntry.COLUMN_CITY_ID + " integer not null, "
                + WeatherContract.WeatherEntry.COLUMN_PRESSURE + " real not null, "
                + WeatherContract.WeatherEntry.COLUMN_WIND_SPEED + " real not null, "
                + WeatherContract.WeatherEntry.COLUMN_WIND_DEG + " integer not null, "
                + "foreign key (" + WeatherContract.WeatherEntry.COLUMN_CITY_ID + ") references "
                + WeatherContract.LocationEntry.TABLE_NAME + " (" + WeatherContract.LocationEntry._ID + ")"
                + " );";
        db.execSQL(SQL_CREATE_LOCATION_TABLE);
        db.execSQL(SQL_CREATE_WEATHER_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
